import React, { Component } from 'react'
//引入connect用于连接UI组件与redux
import {connect} from 'react-redux'
import {
  increment,
  decrement,
  incrementAsync} from '../../redux/actions/count'





// 定义UI组件
class CountUI extends Component {

  state ={carName:'奔驰'}

  increment = ()=>{
    const {value} = this.selectNumber
    this.props.increment(value*1)
  }
  decrement = ()=>{
    const {value} = this.selectNumber
    this.props.decrement(value*1)
  }
  incrementOdd = ()=>{
    const {value} = this.selectNumber
    if (this.props.count%2) {
      this.props.increment(value*1)
    }
  }
  incrementWait = ()=>{
    const {value} = this.selectNumber
    this.props.incrementAsync(value*1,500)
  }


  render() {
    return (
      <div>
        <h1>我是Count组件---下方组件人数为{this.props.persons.length}</h1>
        <h1>当前求和为:{this.props.count}</h1>
        <select ref={(c)=>{this.selectNumber = c}}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
        <button onClick={this.increment}>加</button>
        <button onClick={this.decrement}>减</button>
        <button onClick={this.incrementOdd}>奇数加</button>
        <button onClick={this.incrementWait}>等一等加</button>
      </div>
    )
  }
}

// 创建并暴露一个Count的容器组件
export default connect(
  state => ({
    count:state.count,
    persons:state.persons
  }),
  {
    increment,
    decrement,
    incrementAsync,
  }
)(CountUI)